package com.sheyu.mall.product.service.attr.impl;

import com.sheyu.mall.product.service.attr.vo.AttrGroupRelationVo;
import com.sheyu.mall.product.service.attr.vo.AttrGroupwithAttrsVo;
import com.sheyu.mall.product.vo.AttrGroupVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.product.dao.attr.AttrAttrgroupRelationDao;
import com.sheyu.mall.product.entity.attr.AttrAttrgroupRelationEntity;
import com.sheyu.mall.product.service.attr.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void deleteRealtion(AttrGroupRelationVo[] attrGroupRelationVo) {
        //主要应用逻辑删除操作
        baseMapper.deleteBatchRelationIds( Arrays.asList(attrGroupRelationVo));
    }

    @Override
    public void saveAttrGroupRelations(List<AttrGroupRelationVo> attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrGroupRelationVos.stream().map((obj)->{
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(obj.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(obj.getAttrGroupId());
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(attrAttrgroupRelationEntities);
        baseMapper.saveBatchBySQL(attrAttrgroupRelationEntities);
    }

}
